export default {
    name: 'cart',
    data() {
        return {
            selected: true,
            carts: [],
            multipleSelection: [],
            dialogVisible: false,
            detailList: [],
        }
    },
    created() {
        this.axios.defaults.headers.common['Authorization'] = localStorage['token']
        this.axios.get('api/getCarts')
            .then(res => {
                console.log('carts:', res.data)
                this.carts = res.data.data
            })
    },
    methods: {
        indexMethod(index) {
            return index + 1;
        },
        getDetail(e) {
            this.detailList = []
            console.log('getDetail:', e)
            this.dialogVisible = true
            // this.showDetail = !this.showDetail
            // this.detailList = e
            this.detailList.push(e)
        },
        handleClose(done) {
            done();
        },
        clear() {
            this.$confirm('此操作将清空购物车, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.defaults.headers.common['Authorization'] = localStorage['token']
                this.axios.delete('api/deleteCartsAll', {})
                    .then(res => {
                        this.$message({
                            type: 'success',
                            message: '清空成功',
                            duration: 1000
                        });
                    })
                    .catch(err => {
                        this.$message({
                            type: 'error',
                            duration: 1000,
                            message: '删除失败'
                        });
                    })
                this.carts = [];
            }).catch(() => {
            });
        },
        handleSelectionChange(val) {
            console.log('val:', val)
            this.multipleSelection = val;
        },
        handleAdd(row) {
            if (row.num < 100) {
                row.num++
            } else {
                this.$confirm('已经添加100本该图书了,仍要继续添加吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    row.num++
                }).catch(() => {
                });
            }
        },
        handleMinus(row) {
            if (row.num > 1) {
                row.num--
            } else {
                this.$message({
                    type: 'error',
                    duration: 1200,
                    message: '不能再减少了'
                });
            }
        },
        handleDelete(index) {
            let id = this.carts[index]._id
            this.carts.splice(index, 1)
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.delete('api/deleteCart', { params: { id } })
                .then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功',
                        duration: 1000
                    });
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '删除失败'
                    });
                })
        },
        toOrder() {
            if (!this.selected) {
                this.$message({
                    type: 'error',
                    duration: 1000,
                    message: '未选中商品'
                });
                return
            }
            this.$router.push({ path: '/index/setOrder', query: { data: JSON.stringify(this.multipleSelection) } })
        }
    },
    computed: {
        TotalPrice() {
            let total = 0
            this.multipleSelection.map(item => {
                total += parseInt(item.product.price) * parseInt(item.num)
            })
            if (total > 0) {
                this.selected = true
            } else {
                this.selected = false
            }
            return total
        }
    },
}